/**
 * @description 登录页面对应的属性组件
 */
import React, { useEffect, useMemo, useState, useContext, useRef } from 'react'
import { Collapse, Form, Select, Input, Radio } from 'antd';

import { replaceObjectStyle, replaceCssStyle } from '@/utils/style';
import AnimateOptions from '@/components/StylesWrapper/AnimateOptions.js';
import { setAttrForm } from '@/utils/AttrForm';
import { copyTemplate, pasteTemplate, copyBlock, pasteBlock } from '@/utils/clipboard';
import { FlagOptions } from '@/utils/Const';
import { UpdateContext } from '@/store/updateContext';

import FileUpload from '@/components/StylesWrapper/FileUpload';
import StyleWrapper from '@/components/StylesWrapper';
import CssEdit from '@/components/Monaco/CssEidt';
import AttrHeaderBtn from './Components/AttrHeaderBtn';
import AttrExtra from './Components/AttrExtra';


// 背景类型的选项
const bgOptions = [
  {
    value: '1',
    label: '默认背景',
  },
  // {
  //   value: '2',
  //   label: '动画背景',
  // },
  // {
  //   value: '3',
  //   label: '视频背景',
  // },
  // {
  //   value: '4',
  //   label: '轮播背景',
  // },
]

// 表单布局
const layoutOption = [
  {
    value: 'horizontal',
    label: '水平'
  },
  {
    value: 'vertical',
    label: '垂直'
  },
]

// 标题和后缀现实的开关
const titleShowOptions = [
  {
    label: '开',
    value: true
  },
  {
    label: '关',
    value: false
  }
]





function RegisterAttr({ template2, onConfigEvent, setIsRightAttr, actKey }) {

  const updateCtx = useContext(UpdateContext)
  const [form] = Form.useForm()
  // css编辑器
  const [open, setOpen] = useState(false)
  // css编辑器实例
  const cssEditRef = useRef()

  // 更新style样式文件
  let upateStyleEvent = (keys) => {

    let divData = Object.assign({}, form.getFieldValue(keys))

    // keys数组的最后一项，就是key
    let key = keys.at(-1)
    replaceObjectStyle(actKey, key, divData)
  }

  // 组件css变化
  let cssChange = (value) => {
    form.setFieldValue('css', value)
    replaceCssStyle(actKey, value)
  }

  // 头部按钮事件
  let handleHeaderEvent = (key) => {
    switch (key) {

      // 打开组件css编辑
      case 'css':
        setOpen(true)
        break;

      // 复制模板
      case 'copy':
        copyTemplate(actKey, [], form)
        break;

      // 黏贴模板
      case 'paste':
        pasteTemplate(form, onConfigEvent, actKey)
        updateCtx.setUpdateFlag(pre => !pre)

        // 修改css编辑器的值
        cssEditRef.current.setValue(form.getFieldValue('css'))

        break;

      // 切换位置
      case 'position':
        setIsRightAttr((isRight) => !isRight)
        break;

      default:
        break;
    }
  }

  // 属性栏右上角按钮事件
  let handleAttrEvent = (eventKey, blockKey, paramKeys, classKey) => {
    switch (eventKey) {

      // 复制模块
      case 'copy':
        copyBlock(actKey, blockKey, paramKeys, classKey, form)
        break;

      // 黏贴模块
      case 'paste':
        pasteBlock(actKey, blockKey, paramKeys, classKey, form, onConfigEvent)
        updateCtx.setUpdateFlag(pre => !pre)
        break;


      default:
        break;
    }
  }
  // -----------------------------------
  // 构造折叠面板的数据
  // -----------------------------------

  let items = useMemo(() => {
    const items = [
      {
        key: 'bg',
        label: '背景',
        children: <Collapse accordion size='middle' items={[
          {
            key: 'type',
            label: '背景配置',
            children:
              <>
                <Form.Item
                  label="背景类型"
                  name={['base', 'bg_type']}
                >
                  <Select
                    options={bgOptions}
                  >
                  </Select>
                </Form.Item >
                <FileUpload form={form} label='背景图片' name={['base', 'background_image']} onConfigEvent={onConfigEvent} />

              </>


          },
          {
            key: 'bg',
            label: '默认背景属性',
            children: <StyleWrapper form={form} preKeys={['bg', 'default']} onStyleChange={() => upateStyleEvent(['bg'])} />,
          }
        ]} />,
      },
      {
        key: 'form',
        label: '表单盒子',
        children:
          (
            <>
              <Form.Item
                label="动画属性"
                name={['base', 'form_animate']}
              >
                <Select
                  allowClear
                  options={AnimateOptions}
                  onChange={(value) => onConfigEvent(['base', 'form_animate'], value)}
                />
              </Form.Item>
              <Form.Item
                label="布局"
                name={['registerform', 'base', 'layout']}
              >
                <Radio.Group
                  options={layoutOption}
                  onChange={(e) => onConfigEvent(['registerform', 'base', 'layout'], e.target.value)}
                  optionType="button"
                  buttonStyle="solid"
                />
              </Form.Item>

              <StyleWrapper form={form} preKeys={['form', 'default']} onStyleChange={() => upateStyleEvent(['form'])} />
            </>

          )
        ,
      },
      {
        key: 'form_wrapper',
        label: '表单盒子2',
        children: <StyleWrapper form={form} preKeys={['form_wrapper', 'default']} onStyleChange={() => upateStyleEvent(['form_wrapper'])} />,
      },
      {
        key: 'title',
        label: '标题',
        children:
          <>

            <Form.Item
              label="开关"
              name={['base', 'title_flag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'title_flag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>

            <Form.Item
              label="后缀"
              name={['base', 'title_suffixFlag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'title_suffixFlag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>

            <Form.Item
              label="后缀文字"
              name={['base', 'title_suffix']}
            >
              <Input
                onChange={(e) => onConfigEvent(['base', 'title_suffix'], e.target.value)}
              />
            </Form.Item>

            <StyleWrapper form={form} preKeys={['title', 'default']} onStyleChange={() => upateStyleEvent(['title'])} />

          </>
      },
      {
        key: "input",
        label: "输入框",
        extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'form', ['registerform'], 'registerform')} />,
        children:
          <Collapse accordion size='middle' items={[
            {
              key: 'item',
              label: 'item盒子',
              children: <StyleWrapper form={form} preKeys={['registerform', 'registerform_item', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_item'])} />,
            },
            {
              key: 'label',
              label: '标签',
              children:
                <>
                  <Form.Item
                    label="开关"
                    name={['registerform', 'base', 'labelFlag']}
                  >
                    <Radio.Group
                      options={FlagOptions}
                      onChange={(e) => onConfigEvent(['registerform', 'base', 'labelFlag'], e.target.value)}
                      optionType="button"
                      buttonStyle="solid"
                    />
                  </Form.Item>

                  <Form.Item
                    label="冒号开关"
                    name={['registerform', 'base', 'colonFlag']}
                  >
                    <Radio.Group
                      options={FlagOptions}
                      onChange={(e) => onConfigEvent(['registerform', 'base', 'colonFlag'], e.target.value)}
                      optionType="button"
                      buttonStyle="solid"
                    />
                  </Form.Item>

                  <StyleWrapper form={form} preKeys={['registerform', 'registerform_label', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_label'])} />

                </>
            },
            {
              key: 'input',
              label: '普通',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'input',
                    label: '普通',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_input', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_input'])} />,
                  },
                  {
                    key: 'input-focus',
                    label: '聚焦',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_input', 'focus']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_input'])} />,
                  },
                  {
                    key: 'input-icon',
                    label: '图标',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_input_icon', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_input_icon'])} />,
                  },
                  {
                    key: 'input-icon-hover',
                    label: '图标-hover',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_input_icon', 'hover']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_input_icon'])} />,
                  },
                ]} />
            },
            {
              key: 'input_placeholder',
              label: '输入框的提示语',
              children: <StyleWrapper form={form} preKeys={['registerform', 'registerform_input_placeholder', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_input_placeholder'])} />,
            },
            {
              key: 'input_error',
              label: '输入框错误时',
              children:
                <StyleWrapper form={form} preKeys={['registerform', 'registerform_input_error', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_input_error'])} />
            },
            {
              key: 'number_input',
              label: '数值',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'input',
                    label: '数值',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_number', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_number'])} />,
                  },
                  {
                    key: 'input-focus',
                    label: '聚焦',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_number', 'focus']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_number'])} />,
                  },
                  {
                    key: 'input-icon',
                    label: '图标盒子',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_number_icon_box', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_number_icon_box'])} />,
                  },
                  {
                    key: 'input-icon-up',
                    label: '上图标',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_number_icon_up', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_number_icon_up'])} />,
                  },
                  {
                    key: 'input-icon-down',
                    label: '下图标',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_number_icon_down', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_number_icon_down'])} />,
                  },
                ]} />
            },
            {
              key: 'select',
              label: '单选',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'input',
                    label: '单选',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_select', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_select'])} />,
                  },
                  {
                    key: 'focus',
                    label: '聚焦',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_select', 'focus']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_select'])} />,
                  },
                  {
                    key: 'icon',
                    label: '图标',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_select_icon', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_select_icon'])} />,
                  },
                ]} />
            },
            {
              key: 'date',
              label: '日期',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'input',
                    label: '日期',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_date', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_date'])} />,
                  },
                  {
                    key: 'focus',
                    label: '聚焦',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_date', 'focus']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_date'])} />,
                  },
                  {
                    key: 'icon',
                    label: '图标',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_date_icon', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_date_icon'])} />,
                  },
                ]} />
            },
            {
              key: 'face',
              label: '人脸登录',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'button',
                    label: '按钮',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_face', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_face'])} />,
                  },
                  {
                    key: 'hover',
                    label: 'hover',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_face', 'hover']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_face'])} />,
                  },
                ]} />
            },
            {
              key: 'fileupload',
              label: '文件上传',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'button',
                    label: '盒子',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_fileupload', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_fileupload'])} />,
                  },
                  {
                    key: 'hover',
                    label: 'hover',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_fileupload', 'hover']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_fileupload'])} />,
                  },
                ]} />
            },
            {
              key: 'textarea',
              label: '多行文本',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'input',
                    label: 'input',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_textarea', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_textarea'])} />,
                  },
                  {
                    key: 'icon',
                    label: 'focus',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_textarea', 'focus']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_textarea'])} />,
                  },
                ]} />
            },
            {
              key: 'sms',
              label: '发送验证码',
              children:
                <Collapse accordion size='middle' items={[
                  {
                    key: 'btn',
                    label: '按钮',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_smsbtn', 'default']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_smsbtn'])} />
                  },
                  {
                    key: 'btn-hover',
                    label: '按钮-悬浮',
                    children:
                      <StyleWrapper form={form} preKeys={['registerform', 'registerform_smsbtn', 'hover']} onStyleChange={() => upateStyleEvent(['registerform', 'registerform_smsbtn'])} />
                  },
                ]} />
            },
          ]} />
      },
      {
        key: 'btn',
        label: '按钮',
        children:
          <Collapse accordion size='middle' items={[
            {
              key: 'btn_box',
              label: '盒子',
              children:
                <StyleWrapper form={form} preKeys={['btn_box', 'default']} onStyleChange={() => upateStyleEvent(['btn_box'])} />
            },
            {
              key: 'register_box',
              label: '注册盒子',
              children:
                <>
                  <Form.Item
                    label="文本"
                    name={['base', 'register_text']}
                  >
                    <Input
                      onChange={(e) => onConfigEvent(['base', 'register_text'], e.target.value)}
                    />
                  </Form.Item>

                  <StyleWrapper form={form} preKeys={['register_box', 'default']} onStyleChange={() => upateStyleEvent(['register_box'])} />

                </>
            },
            {
              key: 'register',
              label: '注册',
              children:
                <StyleWrapper form={form} preKeys={['register', 'default']} onStyleChange={() => upateStyleEvent(['register'])} />
            },
            {
              key: 'register_hover',
              label: '注册hover',
              children:
                <StyleWrapper form={form} preKeys={['register', 'hover']} onStyleChange={() => upateStyleEvent(['register'])} />
            },
            {
              key: 'login_box',
              label: '登录盒子',
              children:
                <>
                  <Form.Item
                    label="文本"
                    name={['base', 'login_text']}
                  >
                    <Input
                      onChange={(e) => onConfigEvent(['base', 'login_text'], e.target.value)}
                    />
                  </Form.Item>

                  <StyleWrapper form={form} preKeys={['login_box', 'default']} onStyleChange={() => upateStyleEvent(['login_box'])} />

                </>
            },
            {
              key: 'login',
              label: '登录',
              children:
                <StyleWrapper form={form} preKeys={['login', 'default']} onStyleChange={() => upateStyleEvent(['login'])} />
            },
            {
              key: 'login_hover',
              label: '登录hover',
              children:
                <StyleWrapper form={form} preKeys={['login', 'hover']} onStyleChange={() => upateStyleEvent(['login'])} />
            },
          ]} />
      },
      {
        key: 'idea_1',
        label: '自定义盒子1',
        children:
          <>
            <Form.Item
              label="开关"
              name={['base', 'idea1_flag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'idea1_flag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>
            <Form.Item
              label="文本"
              name={['base', 'idea1_text']}
            >
              <Input
                onChange={(e) => onConfigEvent(['base', 'idea1_text'], e.target.value)}
              />
            </Form.Item>
            <StyleWrapper form={form} preKeys={['idea_1', 'default']} onStyleChange={() => upateStyleEvent(['idea_1'])} />

          </>
      },
      {
        key: 'idea_2',
        label: '自定义盒子2',
        children:
          <>
            <Form.Item
              label="开关"
              name={['base', 'idea2_flag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'idea2_flag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>
            <Form.Item
              label="文本"
              name={['base', 'idea2_text']}
            >
              <Input
                onChange={(e) => onConfigEvent(['base', 'idea2_text'], e.target.value)}
              />
            </Form.Item>
            <StyleWrapper form={form} preKeys={['idea_2', 'default']} onStyleChange={() => upateStyleEvent(['idea_2'])} />

          </>
      },
    ]
    return items
  }, [updateCtx.updateFlag])



  useEffect(() => {
    setAttrForm(form)
  }, [form])

  return (
    <>

      <Form
        name='register'
        form={form}
        initialValues={template2}
        labelCol={{
          span: 6,
        }
        }
        autoComplete="off"
        spellCheck="false"
      >

        {/* 按钮区域 */}
        <AttrHeaderBtn onClickEvent={handleHeaderEvent} />

        {/* 对象形式的样式编辑 */}
        <Collapse accordion destroyInactivePanel size='middle' items={items} />


      </Form>

      {/* 字符串形式的样式编辑 */}
      <CssEdit value={template2.css} open={open} onValueChange={cssChange} setOpen={setOpen} ref={cssEditRef} />

    </>

  )
}
export default React.memo(RegisterAttr) 